import java.util.*;

/**
 * Created with Intellij JDEA
 * Description:
 * User:
 * Date:2022-04-25
 * Time:10:40
 */
public class 随机数索引 {
    class Solution {
        public int[] nums;
        Random random;

        public Solution(int[] nums) {
            this.nums = nums;
            random = new Random();
        }

        public int pick(int target) {
            Stack<Integer> stack = new Stack<>();
            int count = 0;
            int ret = 0;
            for(int i = 0;i < nums.length;i++) {
                if(nums[i] == target) {
                    stack.push(i);
                    count++;
                }
            }
            int num = random.nextInt(count);
            for(int i = 0;i < num;i++) {
                stack.pop();
            }
            ret = stack.pop();
            return ret;
        }
    }
    class Solution2 {
        public int[] nums;
        Random random;

        public Solution2(int[] nums) {
            this.nums = nums;
            random = new Random();
        }

        public int pick(int target) {
            int count = 0;
            int ret = 0;
            for(int i = 0;i < nums.length;i++) {
                if(nums[i] == target) {
                    count++;
                    if (random.nextInt(count) == 0) {
                        ret = i;
                    }
                }
            }
            return ret;
        }
    }
}
